﻿using LittleBee.IDAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LittleBee.CellStore.Dialog
{
	public partial class ManageCellFrame : Form
	{
		public object? CellDataSource { get; internal set; }

		readonly LBAccess _dba = LBAccess.Instance;

		public ManageCellFrame()
		{
			InitializeComponent();
		}

		private void ManageCellFrame_Load(object sender, EventArgs e)
		{
			listBox1.DataSource = CellDataSource;
			listBox1.DisplayMember = "type";
			listBox1.ValueMember = "cell_id";
		}

		private void btDelete_Click(object sender, EventArgs e)
		{
			if (CellDataSource == null)
				return;

			DataTable dtCell = ((CellDataSource as BindingSource)!.DataSource as DataTable)!;

			if (listBox1.SelectionMode == SelectionMode.MultiSimple)
			{
				if (listBox1.SelectedIndices.Count > 0)
				{
					foreach (var idx in listBox1.SelectedIndices)
					{
						int nIndex = (int)idx;
						DataRow dwCell = dtCell.Rows[nIndex];
						_dba.StoreDeleteCellType(dwCell.Field<Int64>("cell_id"));
						dtCell.Rows.RemoveAt(listBox1.SelectedIndex);
					}
				}
			}
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (listBox1.Items.Count > 0 && listBox1.SelectedIndices.Count > 0)
				btDelete.Enabled = true;
			else
				btDelete.Enabled = false;
		}
	}
}
